package com.gite.nouette.admin.implementation.observer;

import java.util.ArrayList;
import java.util.List;

import com.gite.nouette.admin.contract.observer.IObservable;
import com.gite.nouette.admin.contract.observer.IObserver;

public class ObservableImpl<O> implements IObservable<O> {
	private List<IObserver<O>> observers = null;
	private O o;

	public ObservableImpl() {
		setObservers(new ArrayList<IObserver<O>>());
	}

	public ObservableImpl(O o) {
		this();
		setO(o);
	}

	private List<IObserver<O>> getObservers() {
		return this.observers;
	}

	private void setObservers(List<IObserver<O>> observers) {
		this.observers = observers;
	}

	public O getO() {
		return this.o;
	}

	public void setO(O o) {
		this.o = o;
	}

	@Override
	public void addObserver(IObserver<O> observer) {
		getObservers().add(observer);
	}

	@Override
	public int countObservers() {
		return getObservers().size();
	}

	@Override
	public void deleteObserver(IObserver<O> observer) {
		getObservers().remove(observer);
	}

	@Override
	public void notifyObservers() {
		for (IObserver<O> observer : getObservers()) {
			observer.update(getO());
		}
	}
}
